package logic

import (
	"context"
	"errors"
	"hem/hemrpc/hemrpc"
	"hem/hemrpc/internal/svc"
	"hem/models/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type HemShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHemShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HemShowLogic {
	return &HemShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HemShowLogic) HemShow(in *hemrpc.HemShowRequest) (*hemrpc.HemShowResponse, error) {
	// todo: add your logic here and delete this line
	var hem mysql.Hem
	mysql.DB.Debug().Where("id=?", in.HemId).Find(&hem)
	if in.HemId == 0 {
		return nil, errors.New("商品不存在")
	}

	return &hemrpc.HemShowResponse{
		Title: hem.Title,
		Image: hem.Image,
		Price: float32(hem.Price),
		Num:   int64(hem.Num),
	}, nil
}
